/*
 * Qujie.com Inc.
 * Copyright (c) 2010-2066 All Rights Reserved.
 */
package com.insigma.business.qrcode.common._consumer.impl;


import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.insigma.business.common.utils.ToolsHelper;
import com.insigma.business.qrcode.common.dao.CommonAdminAreaDO;
import com.insigma.business.qrcode.common.dao.SoldierInsertLogDao;
import com.insigma.business.qrcode.common.dto.*;
import com.insigma.business.qrcode.common._bff.CommonBffProvider;
import com.insigma.business.qrcode.common._consumer.CommonConsumer;
import com.insigma.business.qrcode.common.dto.req.QueryAreaStreetReqDTO;
import com.insigma.business.qrcode.common.dto.req.QueryOperateLogReqDTO;
import com.insigma.business.qrcode.common.dto.res.QueryOperateLogResDTO;
import com.insigma.business.qrcode.common.service.CommonAdminAreaService;
import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.ResponseUtils;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.common.vo.BaseResponseExtend;
import com.insigma.business.qrcode.common.vo.ComonAttachmentRequest;
import com.insigma.business.qrcode.intell.entity.CareSoldierDemandDO;
import com.insigma.business.qrcode.merchant.dto.request.CareWelfareMerchantInfoSearchRequest;
import com.insigma.business.qrcode.soldier.dto.*;
import com.insigma.business.qrcode.soldier.dto.QaResult;
import com.insigma.business.qrcode.soldier.request.*;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * @author watson.Guo
 * @version $Id: SecurityConsumerImpl.java, v 0.1 2020/4/9 10:58 watson.Guo Exp $
 */
@Service("commonConsumer")
public class CommonConsumerImpl implements CommonConsumer {

    @Resource
    private CommonBffProvider commonBffProvider;

    @Autowired
    private CommonAdminAreaService commonAdminAreaService;

    @Autowired
    private SoldierInsertLogDao soldierInsertLogDao;

    @Override
    public String insertCommonAttachment(ComonAttachmentRequest request) {
        BizCheckUtil.checkNotNull(request, "request不能空");
        BizCheckUtil.checkNotNull(request.getBucket(), "bBucket不能为空");
        BizCheckUtil.checkNotNull(request.getObjectKey(), "ObjectKey不能空");
        BizCheckUtil.checkNotNull(request.getRelativeUri(), "RelativeUri不能空");


        String result = commonBffProvider.insertCommonAttachment(request).getDto();
        return result;
    }

    @Override
    public SystemConfigResult getSystemConfigByName(String name) {
        BizCheckUtil.checkNotNull(name, "name不能空");
        return commonBffProvider.getSystemConfigByName(name).getDto();
    }

    @Override
    public List<PermissionRoleDTO> queryAllRoleDetail() {
        return null;
    }

    @Override
    public BasePagingResponse<List<CityPlatformSoldierBasicInfoDTO>> searchPreferentialTreatment(Integer pageNo, Integer pageSize, String idCardNo) {
        return null;
    }

    @Override
    public QaResult intelligentQA(CommonQaInfoSearchRequest searchRequest) {
        return null;
    }

    @Override
    public BasePagingResponse<List<CareSoldierDemandDTO>> searchOpenDemand(CareSoldierDemandSearchRequest request) {
        return null;
    }

    @Override
    public BasePagingResponse<List<CareSoldierVisitedRecordDTO>> searchOpenVisitRecord(CareSoldierVisitedRecordSearchRequest careSoldierVisitedRecordSearchRequest) {
        return null;
    }

    @Override
    public BasePagingResponse<List<OpenMerchantInfoDTO>> searchOpenMerchantInfo(CareWelfareMerchantInfoSearchRequest careWelfareMerchantInfoSearchRequest) {
        return null;
    }

    @Override
    public BasePagingResponse<List<SoldierBasicOpenInfoDTO>> searchOpenBasicInfo(SoldierBasicInfoSearchRequest soldierBasicInfoSearchRequest) {
        return null;
    }

    @Override
    public BasePagingResponse<List<SoldierDifficultyRegistrationInfoDTO>> searchOpenDifficultyRegistrationInfo(SoldierDifficultyRegistrationInfoSearchRequest request) {
        return null;
    }

    @Override
    public BasePagingResponse<List<SoldierUnemploymentRegistrationInfoDTO>> searchOpenUnemploymentRegistrationInfo(SoldierUnemploymentRegistrationInfoSearchRequest request) {
        return null;
    }

    @Override
    public BasePagingResponse<List<SoldierBasicOpenInfoDTO>> searchElderlySoldier(SoldierBasicInfoSearchRequest soldierBasicInfoSearchRequest) {
        return null;
    }

    @Override
    public BaseResponse<List<String>> listAllIdCardNo() {
        return null;
    }

    @Override
    public BaseResponse<List<Tree<String>>> queryAreaStreet(QueryAreaStreetReqDTO queryAreaStreetReqDTO) {
        //level为0将结果处理变成树形返回
        if (null != queryAreaStreetReqDTO.getLevel() && 0 == queryAreaStreetReqDTO.getLevel()) {
            List<CommonAdminAreaDO> subAreaByParentIDAndLevel = commonAdminAreaService.getSubAreaByParentIDAndLevel(new QueryAreaStreetReqDTO());
            //节点配置
            TreeNodeConfig nodeConfig = new TreeNodeConfig();
            nodeConfig.setIdKey("id");
            nodeConfig.setNameKey("value");
            nodeConfig.setParentIdKey("parentId");
            //构建树
            List<Tree<String>> build = TreeUtil.build(subAreaByParentIDAndLevel, "0", nodeConfig, (treeNode, tree) -> {
                tree.setId(treeNode.getId());
                tree.setName(treeNode.getValue());
                tree.setParentId(treeNode.getParentId());
                //扩展属性
                tree.putExtra("fullValuePath",treeNode.getFullValuePath());
            });
            return BaseResponseExtend.success(build);
        }
        //其他方式按照层级来分次查询
        if (StringUtils.isBlank(queryAreaStreetReqDTO.getParentId())) {
            queryAreaStreetReqDTO.setLevel(1);
        }
        List<CommonAdminAreaDO> subAreaByParentIDAndLevel = commonAdminAreaService.getSubAreaByParentIDAndLevel(queryAreaStreetReqDTO);
        if (CollectionUtils.isNotEmpty(subAreaByParentIDAndLevel)) {
            //节点配置
            TreeNodeConfig nodeConfig = new TreeNodeConfig();
            nodeConfig.setIdKey("id");
            nodeConfig.setNameKey("value");
            nodeConfig.setParentIdKey("parentId");
            //构建树
            List<Tree<String>> build = TreeUtil.build(subAreaByParentIDAndLevel, subAreaByParentIDAndLevel.get(0).getParentId(), nodeConfig, (treeNode, tree) -> {
                tree.setId(treeNode.getId());
                tree.setName(treeNode.getValue());
                tree.setParentId(treeNode.getParentId());
                //扩展属性
                tree.putExtra("fullValuePath",treeNode.getFullValuePath());
            });
            return BaseResponseExtend.success(build);
        }
        return BaseResponseExtend.success(Collections.emptyList());
    }

    @Override
    public BaseResponse<List<QueryOperateLogResDTO>> queryOperateLog(QueryOperateLogReqDTO reqDTO) {
        PageHelper.startPage(reqDTO.getPageNo(), reqDTO.getPageSize());
        final List<SoldierInsertLogDO> soldierInsertLogDOS = soldierInsertLogDao.selectAll();
        PageInfo<SoldierInsertLogDO> pageinfo = new PageInfo<>(soldierInsertLogDOS);
        List<QueryOperateLogResDTO> queryOperateLogResDTOS = ToolsHelper.copyListProperties(soldierInsertLogDOS, QueryOperateLogResDTO::new);
        return ResponseUtils.buildSuccessPagingResponse(new BasePagingResponse<>(), queryOperateLogResDTOS, reqDTO, pageinfo.getTotal());
    }


}
